# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.indusItemDao import IndusItemDao

from . import app

CORS(app, supports_credentials=True)


@app.route('/insertIndustryItem', methods=['POST'])
def insert_industry_item():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    info = {
        'industryId': request.json.get('industryId', ''),
        'itemId': request.json.get('itemId', '')
    }

    info = IndusItemDao().insertIndusItem(info)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findIndustryItemById', methods=['POST'])
def find_industry_item_by_id():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    indusId = request.json.get('indusId', '')
    info = IndusItemDao().findIndusItemById(indusId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findIndustryItemByCondition', methods=['POST'])
def find_industry_item_by_condition():
    if not request.json:
        abort(400)
    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})
    try:
        conditions = request.json['condition']
        # field = conditions['field']
        # op = conditions['op']
        # value = conditions['value']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']
        # sortField = multiSort['field']
        # sort = multiSort['sort']
    except:
        return jsonify({"message": "未传入排序规则multiSort"})
    info = IndusItemDao().findIndusItemByCondition(pageIndex=pageIndex, pageSize=pageSize,conditions=conditions, multiSort=multiSort)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/deleteIndustryItemByIds', methods=['POST'])
def delete_industry_item_by_ids():
    """
    根据ID删除，ID以数组形式传递
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [0])

    info = IndusItemDao().deleteIndus(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateIndustryItemById', methods=['POST'])
def update_industry_item_by_id():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        id = request.json['id']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入要修改信息的ID"}})
    oldInfo = IndusItemDao().selectIndusItem(id)
    info = {
        'id': request.json['id'],
        'industryId': request.json.get('industryId', oldInfo[1]),
        'itemId': request.json.get('itemId', oldInfo[2])
    }

    info = IndusItemDao().updateIndus(info)
    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})
